#include "precompile.h"

#include "Layout.h"

PageLayout::PageLayout(const Size& canvasSize,const Size& pagesize,int pagecount,const Point& viewpoint)
{
	this->canvasSize = canvasSize;
	this->pagesize = pagesize;
	this->pagecount = pagecount;
	this->viewpoint = viewpoint;
}

void PageLayout::Layout()
{
	float width = pagecount*pagesize.cx;
	float two_width = 2*pagesize.cx;
	float one_width = 1*pagesize.cx;
	float one_height = 1*pagesize.cy;
	if (pagecount == 1)
	{
		m_pagelayoutinfo.v_pagecount = 1;
		m_pagelayoutinfo.h_pagecount = 1;
		viewpoint.x = (canvasSize.cx-pagesize.cx)/2;
	}
	if (width <= canvasSize.cx)
	{
		m_pagelayoutinfo.h_pagecount = pagecount;
		m_pagelayoutinfo.v_pagecount = 1;
	}
	else if (two_width <= canvasSize.cx)
	{
		m_pagelayoutinfo.h_pagecount = 2;
		m_pagelayoutinfo.v_pagecount = pagecount/2;
		viewpoint.x = (canvasSize.cx-pagesize.cx*2)/2;
	}
	else if (one_width <= canvasSize.cx)
	{
		m_pagelayoutinfo.h_pagecount = 1;
		m_pagelayoutinfo.v_pagecount = pagecount;
		viewpoint.x = (canvasSize.cx-pagesize.cx*1)/2;
	}
	else
	{
		m_pagelayoutinfo.h_pagecount = 1;
		m_pagelayoutinfo.v_pagecount = pagecount;
		viewpoint.x = (canvasSize.cx-pagesize.cx)/2;
	}

	Point alignpoint = viewpoint;
	for (int i = 1; i <= m_pagelayoutinfo.v_pagecount; ++i)
	{
		viewpoint.x  = alignpoint.x;
		for (int j = 1; j <=  m_pagelayoutinfo.h_pagecount; ++j)
		{
			m_pagelayoutinfo.pages.push_back(PageLayoutEntry(j,i,viewpoint));
			viewpoint.x  += pagesize.cx;
		}
		viewpoint.y += pagesize.cy;
	}
}
const PageLayoutInfo& PageLayout::GetResult() const
{
	return m_pagelayoutinfo;
}